//
//  ImageEffectTestViewController.m
//  TestPrj
//
//  Created by 朱铅峰 on 2021/3/25.
//  Copyright © 2021 Xing. All rights reserved.
//

#import "ImageEffectTestViewController.h"
#import "OpenGLESBaseViewController.h"
#import "GLSL_EffectViewController.h"
#import "LottieTestViewController.h"

@interface ImageEffectTestViewController ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>

proStrong UITableView *tableView;
proStrong UIView *contactView;
proStrong UIControl *contactControl;

proStrong NSArray<NSString *> *dataSource;

@end

@implementation ImageEffectTestViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.title = @"图像处理";
    self.dataSource = @[@"OpenGL ES基础",
                        @"GLSL实现几种抖音特效",
                        @"Lottie动画引擎"];
    
    self.tableView = [[UITableView alloc] init];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    
    self.contactView = [[UIView alloc] initWithFrame:CGRectMake(self.view.width-80, self.view.height-120, 80, 80)];
    [self.view addSubview:self.contactView];
    UILabel *imageView = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 60, 60)];
    imageView.text = @"退出";
    imageView.font = [UIFont boldSystemFontOfSize:20];
    imageView.backgroundColor = [UIColor orangeColor];
    imageView.textColor = [UIColor whiteColor];
    imageView.textAlignment = NSTextAlignmentCenter;
    [self.contactView addSubview:imageView];
    self.contactControl = [[UIControl alloc] initWithFrame:imageView.frame];
    [self.contactControl addTarget:self action:@selector(exit) forControlEvents:UIControlEventTouchUpInside];
    [self.contactView addSubview:self.contactControl];

    UIPanGestureRecognizer* singleTwoTap = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(touchesTwoMoved:)];
    singleTwoTap.delegate = self;
    singleTwoTap.cancelsTouchesInView = YES;
    [self.contactView addGestureRecognizer:singleTwoTap];
}

- (void)exit {
    
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - tableView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = self.dataSource[indexPath.row];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.row) {
        case 0:
        {
            OpenGLESBaseViewController *vc = [OpenGLESBaseViewController new];
            [self.navigationController pushViewController:vc animated:YES];
            break;
        }
        case 1:
        {
            GLSL_EffectViewController *vc = [GLSL_EffectViewController new];
            [self.navigationController pushViewController:vc animated:YES];
            break;
        }
        case 2:
        {
            LottieTestViewController *vc = [LottieTestViewController new];
            [self.navigationController pushViewController:vc animated:YES];
            break;
        }
        default:
            break;
    }
}

#pragma mark -
#pragma mark contact gesture

- (void)touchesTwoMoved:(UIPanGestureRecognizer *)touches {
    
    self.contactControl.enabled = NO;
    
    UIView *view = touches.view;
    if (touches.state == UIGestureRecognizerStateBegan ||
        touches.state == UIGestureRecognizerStateChanged) {
        
        CGPoint translation = [touches translationInView:view.superview];
        [view setCenter:(CGPoint){view.center.x + translation.x, view.center.y + translation.y}];
        [touches setTranslation:CGPointZero inView:view.superview];
    }
    
    if (touches.state == UIGestureRecognizerStateEnded) {
        
        CGFloat left = view.superview.width-view.width;
        CGFloat top = view.top;
        if (top < 0) {
            top = 0;
        } else if (view.top > view.superview.height-view.height-80) {
            top = view.superview.height-view.height-10;
        }
        [UIView animateWithDuration:0.25 animations:^{
            view.frame = CGRectMake(left, top, view.width, view.height);
        }];
        
        self.contactControl.enabled = YES;
    }
}

@end
